package com.dazhao.animejaphome.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;

import com.dazhao.animejaphome.R;

import java.util.LinkedHashMap;

import cn.jzvd.JZMediaManager;
import cn.jzvd.JZUserAction;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * Created by zhao on 2017/10/14.
 */

public class DzVideo extends JZVideoPlayerStandard {

    ImageButton recycle;

    public DzVideo(Context context){
        super(context);
    }
    public DzVideo(Context context, AttributeSet attributeSet){
        super(context,attributeSet);
    }

    @Override
    public void init(Context context) {
        super.init(context);
        recycle=findViewById(R.id.dz_recycle);
        recycle.setOnClickListener(this);

    }

    @Override
    public int getLayoutId() {
        return R.layout.dz_video_layout;
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        //循环按钮的点击事件
        if(i==R.id.dz_recycle){
//            recycle.setImageResource(R.drawable._recycle);

            if (currentState == CURRENT_STATE_NORMAL || currentState == CURRENT_STATE_ERROR) {
                recycle.setImageResource(R.drawable.one_recycle);
                startVideo();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        boolean done=false;
                        while (!done){
                            if(currentState == CURRENT_STATE_PLAYING){
                                JZMediaManager.instance().mediaPlayer.setLooping(true);
                                done=true;
                            }
                        }
                    }
                }).start();
                onEvent(currentState != CURRENT_STATE_ERROR ? JZUserAction.ON_CLICK_START_ICON : JZUserAction.ON_CLICK_START_ERROR);
            } else if (currentState == CURRENT_STATE_PLAYING) {
                onEvent(JZUserAction.ON_CLICK_PAUSE);
                Log.d(TAG, "pauseVideo [" + this.hashCode() + "] ");
                if(JZMediaManager.instance().mediaPlayer.isLooping()){
                    JZMediaManager.instance().mediaPlayer.setLooping(false);
                    recycle.setImageResource(R.drawable._recycle);
                }else{
                    JZMediaManager.instance().mediaPlayer.setLooping(true);
                    recycle.setImageResource(R.drawable.one_recycle);
                }
            } else if (currentState == CURRENT_STATE_PAUSE) {
                onEvent(JZUserAction.ON_CLICK_RESUME);
                JZMediaManager.instance().mediaPlayer.start();
                onStatePlaying();
                if(JZMediaManager.instance().mediaPlayer.isLooping()){
                    JZMediaManager.instance().mediaPlayer.setLooping(false);
                    recycle.setImageResource(R.drawable._recycle);
                }else{
                    JZMediaManager.instance().mediaPlayer.setLooping(true);
                    recycle.setImageResource(R.drawable.one_recycle);
                }
            } else if (currentState == CURRENT_STATE_AUTO_COMPLETE) {
                recycle.setImageResource(R.drawable.one_recycle);
                onEvent(JZUserAction.ON_CLICK_START_AUTO_COMPLETE);
                startVideo();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        boolean done=false;
                        while (!done){
                            if(currentState == CURRENT_STATE_PLAYING){
                                JZMediaManager.instance().mediaPlayer.setLooping(true);
                                done=true;
                            }
                        }
                    }
                }).start();
            }
        }
    }

    @Override
    public void setUp(String url, int screen, Object... objects) {
        super.setUp(url, screen, objects);
    }

    @Override
    public void setUp(LinkedHashMap urlMap, int defaultUrlMapIndex, int screen, Object... objects) {
        super.setUp(urlMap, defaultUrlMapIndex, screen, objects);

    }
}
